package com.foreknow.dao.impl;

import com.foreknow.dao.IBaseDao;
import com.foreknow.entity.Blog;
import com.foreknow.util.DbHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDaoImpl implements IBaseDao<Blog> {
	private static QueryRunner queryRunner = DbHelper.getQueryRunner();

	@Override
	public int delete(String sql, Object... params) throws SQLException {
		return queryRunner.update(sql, params);
	}

	@Override
	public int delete(String sql) throws SQLException {
		return queryRunner.update(sql);
	}

	@Override
	public List<Blog> queryByCondition(String sql) throws SQLException {
		return queryRunner.query(sql, getListHandler());

	}
	@Override
	public List<Blog> queryByCondition(String sql, Object ...params) throws SQLException{
		return queryRunner.query(sql, getListHandler(), params);
	}
	@Override
	public int update(String sql, Object... params) throws SQLException {
		return queryRunner.update(sql, params);

	}

	@Override
	public int update(String sql, Object param) throws SQLException {
		return queryRunner.update(sql, param);
	}

	@SuppressWarnings("deprecation")
	@Override
	public Blog queryOne(String sql, Object param) throws SQLException {
		return queryRunner.query(sql, param, getResultSetHandler());

	}

	@SuppressWarnings("deprecation")
	public Blog queryOne(String sql, Object[] params) throws SQLException {
		return queryRunner.query(sql, params, getResultSetHandler());
	}

	@Override
	public ResultSetHandler<Blog> getResultSetHandler() {
		ResultSetHandler<Blog> handler = new ResultSetHandler<Blog>() {
			@Override
			public Blog handle(ResultSet resultSet) throws SQLException {
				Blog blog = null;
				while (resultSet.next()) {
					Integer id = resultSet.getInt("id");
					String content = resultSet.getString("content");
					String title = resultSet.getString("title");
					String category = resultSet.getString("category");
					String uploadTime = resultSet.getString("uploadTime");
					Integer userId = resultSet.getInt("userId");
					Integer volume = resultSet.getInt("readingVolume");
					String introduction = resultSet.getString("introduction");
					blog = new Blog(id, title, content, uploadTime, userId, category, volume,introduction);
				}
				return blog;
			}
		};
		return handler;
	}

	public ResultSetHandler<List<Blog>> getListHandler() {
		ResultSetHandler<List<Blog>> handler = new ResultSetHandler<List<Blog>>() {
			@Override
			public List<Blog> handle(ResultSet resultSet) throws SQLException {
				List<Blog> blogs = new ArrayList<>();
				while (resultSet.next()) {
					Integer id = resultSet.getInt("id");
					String content = resultSet.getString("content");
					String title = resultSet.getString("title");
					String category = resultSet.getString("category");
					String uploadTime = resultSet.getString("uploadTime");
					Integer userId = resultSet.getInt("userId");
					Integer volume = resultSet.getInt("readingVolume");
					String introduction = resultSet.getString("introduction");
					Blog blog = new Blog(id, title, content, uploadTime, userId, category, volume,introduction);
					blogs.add(blog);
				}
				return blogs;
			}
		};
		return handler;
	}

	@Override
	public int insert(String sql, Object[] params) throws SQLException {
		return queryRunner.update(sql, params);

	}
}
